package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.Organization;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.entity.views.ResDepartment;
import org.apache.ibatis.annotations.*;
import org.aspectj.weaver.ast.Or;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 组织机构 Mapper 接口
 * </p>
 *
 * @author min
 * @since 2022-07-26
 */
public interface OrganizationMapper extends BaseMapper<Organization> {

    @Select("")
    public abstract List<Organization> getOrganiztionForApoint();

    @Select("select id from organization ${ew.customSqlSegment}")
    public abstract List<Long> getOrganiztionId(@Param(Constants.WRAPPER) Wrapper wrapper);

    @Select("select name from organization where id = #{schoolId}")
    public abstract String getSchoolName(@Param("schoolId") Long schoolId);

    @Select({"<script>",
            "select * from (",
            "select t1.`name`,SUM(CASE WHEN t2.id is not null THEN 1 ELSE 0 END) as staff_count from organization as t1 LEFT JOIN organization_staff as t2 on t1.id = t2.organization_id GROUP BY t1.id",
            ") as t",
            "<when test='type!=null and type!=&apos;&apos; '>",
            "and type =${type}",
            "</when>",
            "</script>",
    })
    public List<Organization> getOrganiztionList(@Param("type") Integer type);

    @Select({
            "<script>",
            "select id,name,type,pid,status,",
            "<choose>",
                "<when test='staffName!=null and staffName!=&apos;&apos; '>",
                    "#{staffName} as staffName,",
                "</when>",
                "<otherwise>",
                    "null as staffName,",
                "</otherwise>",
            "</choose>",
            "<choose>",
                "<when test='staffStatus!=null and staffStatus!=&apos;&apos; '>",
                "${staffStatus} as staffStatus,",
                "</when>",
                "<otherwise>",
                "null as staffStatus,",
                "</otherwise>",
            "</choose>",
            "<choose>",
                "<when test='staffType!=null and staffType!=&apos;&apos; '>",
                    "${staffType} as staffType,",
                "</when>",
                "<otherwise>",
                    "null as staffType,",
                "</otherwise>",
            "</choose>",
            "<choose>",
                "<when test='staffJustType!=null and staffJustType!=&apos;&apos; '>",
                    "${staffJustType} as staffJustType,",
                "</when>",
                "<otherwise>",
                    "null as staffJustType",
                "</otherwise>",
            "</choose>",
            "from organization where deleted=0",
            "<when test='status!=null and status!=&apos;&apos; '>",
            "  and status = ${status}",
            "</when>",
            "<when test='type!=null and type!=&apos;&apos; '>",
            "  and type = ${type}",
            "</when>",
            "<when test='schoolName!=null and schoolName!=&apos;&apos; '>",
            "  and name like '%${schoolName}%'",
            "</when>",
            "</script>",
    })
    @Results({
        @Result(property = "id",column = "id"),
        @Result(property = "name",column = "name"),
        @Result(property = "staffList",column = "{schoolId=id,staffStatus=staffStatus,staffType=staffType,staffJustType=staffJustType,staffName=staffName}",many = @Many(select = "com.xmy.cultivate.mapper.OrganizationMapper.getStaffListForOrganiztionId"))
    })
    public List<ResDepartment> getOrganiztionAndStaffList(@Param("schoolName")String schoolName,@Param("staffName")String staffName,@Param("status") Integer status, @Param("type") Integer type,@Param("staffStatus") String staffStatus,@Param("staffType") String staffType,@Param("staffJustType") String staffJustType);


    @Select({
            "<script>",
            "select * from (select t1.*,t2.name as superior_name,t3.`name` as station_name,getSchoolName(t1.school_id) as school_name from staff as t1 LEFT JOIN staff as t2 on t1.superior_id = t2.id LEFT JOIN station as t3 on t1.station_id = t3.id) as t where deleted =0 and status &lt;&gt; 2 and school_id = #{schoolId}",
            "<when test='staffType!=null and staffType!=&apos;&apos; '>",
            "  and staff_type = ${staffType}",
            "</when>",
            "<when test='staffStatus!=null and staffStatus!=&apos;&apos; '>",
            "  and status = ${staffStatus}",
            "</when>",
            "<when test='staffJustType!=null and staffJustType!=&apos;&apos; '>",
            "  and just_type = ${staffJustType}",
            "</when>",
            "<when test='staffName!=null and staffName!=&apos;&apos; '>",
            "  and name like '%${staffName}%'",
            "</when>",
            "</script>",
    })
    public List<Staff> getStaffListForOrganiztionId(Map<String,String> map);

    /**
     * 获取所有子校区id（包含主校区）
     * @param schoolId
     * @param status
     * @return
     */
    @Select("select id from organization where (id = ${schoolId} or pid = ${schoolId}) and `status` = ${status} and type=1 and deleted = 0 order by pid asc")
    public List<Long> getOrganizationChidAll(@Param("schoolId") Long schoolId,@Param("status") Integer status);


    /**
     * 随机一条数据，签到使用
     * @return
     */
    @Select("select * from organization where type =1 and `status` =1 and deleted =0 GROUP BY access_key_id ORDER BY RAND() LIMIT 1")
    public Organization getOneRand();

}
